package cn.echase.ecare.yinfayun.service.impl;

import cn.echase.common.core.exception.EchaseException;
import cn.echase.ecare.yinfayun.domain.dto.RemindSettingDto;
import cn.echase.ecare.yinfayun.domain.entity.OrgConfig;
import cn.echase.ecare.yinfayun.domain.entity.StaffCertificate;
import cn.echase.ecare.yinfayun.domain.mapper.OrgConfigMapper;
import cn.echase.ecare.yinfayun.domain.mapper.StaffCertificateMapper;
import cn.echase.ecare.yinfayun.domain.vo.RemindSettingVo;
import cn.echase.ecare.yinfayun.enums.LicenseRemindEnums;
import cn.echase.ecare.yinfayun.service.IStaffCertificateService;
import cn.hutool.core.bean.BeanUtil;
import cn.hutool.core.convert.Convert;
import cn.hutool.core.util.StrUtil;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import io.azam.ulidj.ULID;
import jakarta.annotation.Resource;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.StringUtils;
import org.dromara.common.satoken.utils.LoginHelper;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.ArrayList;
import java.util.List;

/**
 * @Author: zhangbei@echase.cn
 * @Title: StaffCertificateServiceImpl
 * @ProjectName: ecare-soft
 * @Description: 机构员工证书Service业务层处理
 * @Date: 2023/08/28 14:50
 */
@Slf4j
@Service
public class StaffCertificateServiceImpl implements IStaffCertificateService {
    @Resource
    private StaffCertificateMapper staffCertificateMapper;

    @Resource
    private OrgConfigMapper orgConfigMapper;


    /**
     * 查询机构员工证书
     *
     * @param id 机构员工证书ID
     * @return 机构员工证书
     */
    @Override
    public StaffCertificate selectStaffCertificateById(Long id) {
        return staffCertificateMapper.selectStaffCertificateById(id);
    }

    /**
     * 查询机构员工证书列表
     *
     * @param staffCertificate 机构员工证书
     * @return 机构员工证书
     */
    @Override
    public List<StaffCertificate> selectStaffCertificateList(StaffCertificate staffCertificate) {
        return staffCertificateMapper.selectStaffCertificateList(staffCertificate);
    }

    /**
     * 批量新增机构员工证书
     *
     * @param staffCertificateList 机构员工证书
     * @return 结果
     */
    @Override
    @Transactional
    public void insertStaffCerBatch(List<StaffCertificate> staffCertificateList) {
        for (StaffCertificate staffCertificate : staffCertificateList) {
            staffCertificate.setStaffCerId(ULID.random());
        }
        staffCertificateMapper.insertBatch(staffCertificateList);
    }

    /**
     * 新增机构员工证书
     *
     * @param staffCertificate 机构员工证书
     * @return 结果
     */
    @Transactional
    @Override
    public int insertStaffCertificate(StaffCertificate staffCertificate) {
        staffCertificate.setStaffCerId(ULID.random());
        return staffCertificateMapper.insert(staffCertificate);
    }

    /**
     * 修改机构员工证书
     *
     * @param staffCertificate 机构员工证书
     * @return 结果
     */
    @Override
    @Transactional
    public int updateStaffCertificate(StaffCertificate staffCertificate) {
        return staffCertificateMapper.update(staffCertificate, Wrappers.<StaffCertificate>lambdaUpdate()
                .eq(StrUtil.isNotBlank(staffCertificate.getStaffId()), StaffCertificate::getStaffId, staffCertificate.getStaffId())
                .eq(StrUtil.isNotBlank(staffCertificate.getStaffCerId()), StaffCertificate::getStaffCerId, staffCertificate.getStaffCerId()));
    }


    /**
     * 删除机构员工证书对象
     *
     * @param ids 需要删除的数据ID
     * @return 结果
     */
    @Override
    public int deleteStaffCertificateByIds(String ids) {
        return staffCertificateMapper.deleteStaffCertificateByIds(Convert.toStrArray(ids));
    }

    /**
     * 删除机构员工证书信息
     *
     * @param staffCertificate 机构员工证书ID
     * @return 结果
     */
    @Override
    public int deleteStaffCertificate(StaffCertificate staffCertificate) {
        return staffCertificateMapper.deleteStaffCertificate(staffCertificate);
    }

    /**
     * 查询机构证照提醒设置
     *
     * @return 结果
     */
    @Override
    public RemindSettingVo getRemindSetting() {
        if (StringUtils.isEmpty(LoginHelper.getOrgId())) {
            // Super账号不查询机构证照提醒设置
            return null;
        }
        RemindSettingVo remindSettingVo = new RemindSettingVo();
        // 查询证照到期提醒是否开启
        OrgConfig orgConfigQuery = new OrgConfig();
        orgConfigQuery.setConfigKey(LicenseRemindEnums.LICENSE_REMIND_ENABLE.getConfigKey());
        orgConfigQuery.setDeptId(LoginHelper.getDeptId());
        OrgConfig orgConfig = orgConfigMapper.selectOne(Wrappers.<OrgConfig>lambdaQuery()
                .eq(OrgConfig::getConfigKey, orgConfigQuery.getConfigKey())
                .eq(OrgConfig::getDeptId, orgConfigQuery.getDeptId()), false);
        if (BeanUtil.isEmpty(orgConfig)) {
            // 需初始化证照提醒设置
            return this.getDefaultConfigs(orgConfigQuery);
        }
        remindSettingVo.setRemindEnable(orgConfig.getConfigValue());
        // 查询证照到期提醒天数
        orgConfigQuery.setConfigKey(LicenseRemindEnums.LICENSE_REMIND_CYCLE.getConfigKey());
        orgConfig = orgConfigMapper.selectOne(Wrappers.<OrgConfig>lambdaQuery()
                .eq(OrgConfig::getConfigKey, orgConfigQuery.getConfigKey())
                .eq(OrgConfig::getDeptId, orgConfigQuery.getDeptId()), false);
        remindSettingVo.setRemindCycle(orgConfig.getConfigValue());
        return remindSettingVo;
    }

    /**
     * 编辑机构证照提醒设置
     *
     * @param remindSettingDto 证照提醒设置Dto
     * @return 是否成功
     */
    @Override
    public boolean updateRemindSetting(RemindSettingDto remindSettingDto) {
        OrgConfig orgConfigUpdate = new OrgConfig();
        orgConfigUpdate.setDeptId(LoginHelper.getDeptId());
        orgConfigUpdate.setConfigKey(LicenseRemindEnums.LICENSE_REMIND_ENABLE.getConfigKey());
        orgConfigUpdate.setConfigValue(remindSettingDto.getRemindEnable());
        int i = orgConfigMapper.update(orgConfigUpdate, Wrappers.<OrgConfig>lambdaUpdate()
                .eq(OrgConfig::getDeptId, orgConfigUpdate.getDeptId())
                .eq(OrgConfig::getConfigKey, orgConfigUpdate.getConfigKey()));
        orgConfigUpdate.setConfigKey(LicenseRemindEnums.LICENSE_REMIND_CYCLE.getConfigKey());
        orgConfigUpdate.setConfigValue(remindSettingDto.getRemindCycle());
        int j = orgConfigMapper.update(orgConfigUpdate, Wrappers.<OrgConfig>lambdaUpdate()
                .eq(OrgConfig::getDeptId, orgConfigUpdate.getDeptId())
                .eq(OrgConfig::getConfigKey, orgConfigUpdate.getConfigKey()));
        if (!(i > 0 && j > 0)) {
            return false;
        }
        if ("1".equals(remindSettingDto.getRemindEnable())) {
            StaffCertificate staffCertificateUpdate = new StaffCertificate();
            staffCertificateUpdate.setDeptId(LoginHelper.getDeptId());
            staffCertificateUpdate.setRemindCycle(Integer.valueOf(remindSettingDto.getRemindCycle()));
            staffCertificateMapper.update(staffCertificateUpdate, Wrappers.<StaffCertificate>lambdaUpdate()
                    .eq(StaffCertificate::getDeptId, staffCertificateUpdate.getDeptId())
                    .eq(StaffCertificate::getDeleteFlag, "1"));
        }
        return true;
    }

    /**
     * 初始化证照提醒设置
     *
     * @return 证照提醒设置
     */
    private RemindSettingVo getDefaultConfigs(OrgConfig orgConfigQuery) {
        RemindSettingVo remindSettingVo = new RemindSettingVo();
        List<OrgConfig> configList = new ArrayList<>();
        // 获取枚举类的所有值
        for (LicenseRemindEnums licenseRemindEnums : LicenseRemindEnums.values()) {
            OrgConfig orgConfig = new OrgConfig();
            orgConfig.setUcId(ULID.random());
            orgConfig.setDeptId(orgConfigQuery.getDeptId());
            orgConfig.setConfigKey(licenseRemindEnums.getConfigKey());
            orgConfig.setConfigName(licenseRemindEnums.getConfigName());
            orgConfig.setConfigValue(licenseRemindEnums.getConfigValue());
            configList.add(orgConfig);
        }
        if (orgConfigMapper.insertBatch(configList)) {
            throw new EchaseException(500, "初始化证照提醒设置失败！");
        }
        remindSettingVo.setRemindEnable(LicenseRemindEnums.LICENSE_REMIND_ENABLE.getConfigValue());
        remindSettingVo.setRemindCycle(LicenseRemindEnums.LICENSE_REMIND_CYCLE.getConfigValue());
        return remindSettingVo;
    }
}
